package cn.lg.soar.system.monitor.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.model.UpdateDisabledVo;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.monitor.entity.LogConfig;
import cn.lg.soar.system.monitor.service.LogConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = {"日志配置"})
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/log-config")
public class LogConfigController {

    @Autowired
    private LogConfigService service;

    private final static String SUBJECT = "角色";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated LogConfig entity) {
        AssertUtil.isTrue(this.service.save(entity), "创建日志配置失败");
        return HttpResult.<Serializable>success().setData(entity.getId());
    }

    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated LogConfig entity) {
        AssertUtil.isTrue(this.service.updateById(entity), "更新日志配置失败");
        return HttpResult.success();
    }

    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        AssertUtil.isTrue(this.service.removeByIds(ids), "删除日志配置失败");
        return HttpResult.success();
    }


    @UpdateDisabledMapping
    public HttpResult<Void> updateDisabled(@RequestBody @Validated UpdateDisabledVo vo) {
        AssertUtil.isTrue(
                this.service.update(
                        Wrappers.<LogConfig>lambdaUpdate()
                                .set(LogConfig::getDisabled, vo.getDisabled())
                                .in(LogConfig::getId, vo.getIds())
                ),
                vo.getDisabled() ? "禁用日志配置失败" : "启用日志配置失败"
        );
        return HttpResult.success();
    }

    @DetailMapping
    public HttpResult<LogConfig> detail(@Id Long id) {
        LogConfig entity = this.service.getById(id);
        AssertUtil.notNull(entity, "日志配置不存在");
        return HttpResult.data(entity);
    }

    @QueryMapping
    public HttpResult<PageResult<LogConfig>> query(PageQuery pageQuery, QueryParams params){
        return HttpResult.data(
                new SqueryWapper<>(LogConfig.class).query(this.service, pageQuery, params)
        );
    }

    @ApiOperation("刷新缓存")
    @GetMapping("refresh")
    public HttpResult<PageResult<LogConfig>> refresh(){
        service.refresh();
        return HttpResult.success();
    }
}
